const { STRING } = require('sequelize')
const { db } = require('../core/sequelize')
const { Sequelize, Model, INTEGER, Op, database } = require('sequelize')
// const { Comments } = require('./comments')
const { User } = require('./user')

class Posts extends Model {
    static async delReleaseForm(id) {
        const res = await Posts.destroy({
            where: {
                id: id
            }
        })
        return res
    }
    static async releaseForm(data) {
        const res = await Posts.create(data)
        return res
    }
    static async updateReleaseForm(data) {
        const res = await Posts.update(
            {
                ...data
            },
            {
                where: {
                    id: data.id
                }
            })
        console.log('data :>> ', res[0]);
        if (res[0] === 0) {
            throw new Error('啥也没改呀!')
            return
        }
        return res
    }
    static async getPostsDetila(id, userid) {
        let details = await Posts.findOne({
            where: {
                id,
            },
            include: [{
                model: User
            }],
        })
        return details
    }
    static async getPostsList(pageIndex = 1, pageSize = 10, title = '', userid = '') {

        const whereObj = userid ? { userid } : {}
        // 查数量
        let count = await Posts.count({
            where: {
                // 模糊查询
                title: {
                    [Op.like]: `%${title}%`
                },
                ...whereObj
            },
        })
        let postsList = []
        if (count !== 0) {
            postsList = await Posts.findAll(
                {
                    limit: pageSize,
                    offset: (pageIndex - 1) * pageSize,
                    where: {
                        title: {
                            [Op.like]: `%${title}%`
                        },
                        ...whereObj
                    },
                    attributes: {
                        include: [
                            [
                                Sequelize.literal(`(
                                SELECT COUNT(*) FROM comments AS comments
                                  WHERE
                                  comments.postid = posts.id
                                )`),
                                'totalComment',
                            ]
                        ]
                    },
                    // 关联查询
                    include: [
                        {
                            model: User
                        },
                        // {
                        //     model: Comments,
                        // },
                    ],
                    order: [['updateTime', 'DESC']],

                }
            )
        }
        return { count, pageIndex, pageSize, postsList }
    }
}
Posts.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        userid: Sequelize.STRING, //类型 字符串
        likeCount: { type: INTEGER, field: 'likeCount' }, //类型 字符串
        title: Sequelize.STRING, //类型 字符串
        tags: Sequelize.STRING, //类型 字符串
        content: Sequelize.STRING, //类型 字符串
        createTime: { type: STRING, field: 'create_time' },
        updateTime: { type: STRING, field: 'updateTime' },
        imgs: Sequelize.STRING, //类型 字符串
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'posts', timestamps: false
    },

)
// // 关联评论表 
// hasMany一对多查询
// Posts.hasMany(Comments, {
//     foreignKey: 'postid',
//     sourceKey: 'id'
// });

// 关联用户表
Posts.belongsTo(User, {
    foreignKey: 'userid',
    sourceKey: 'id'
});



module.exports = { Posts }
